<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="租住地址" prop="keyWord">
            <el-input
              v-model="queryParams.keyWord"
              placeholder="请输入租住地址"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="租住地址" align="left" prop="rentAddr_" />
      <el-table-column label="租客信息" align="left">
        <template #default="scope">
          <template v-if="scope.row.rentUserResp">
            {{scope.row.rentUserResp.name}}（{{scope.row.rentUserResp.mobile}}）<span style="color:red;" v-if="scope.row.deleted==1">（已退租）</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="房租金额" align="center" prop="rentAmount" width="120"/>
      <el-table-column label="押金" align="center" prop="depositAmount" width="120"/>
      <el-table-column label="收租周期（月）" align="center" prop="payMonthUnit" width="150"/>
      <el-table-column
        label="生效日期"
        align="center"
        prop="effectTime"
        width="120px"
        >
        <template #default="scope">
          <div>{{ formatDate(scope.row.effectTime, 'YYYY-MM-DD') }}</div>
        </template>
      </el-table-column>
      <el-table-column label="线上支付" align="center" prop="onlinePayFlag" width="120">
        <template #default="scope">
          {{ scope.row.onlinePayFlag ? "是":"否" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('view', scope.row.id)"
          >
            查看
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['zwsj:rent-contract:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 详细 -->
  <RentContractView ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { formatDate } from '@/utils/formatTime'
import download from '@/utils/download'
import { RentContractApi, RentContractVO } from '@/api/zwsj/rentcontract'
import RentContractView from './RentContractView.vue'

/** 房租合同 列表 */
defineOptions({ name: 'RentContract' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<RentContractVO[]>([]) // 列表的数据
// 列表的总页数
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  draftFlag: false,
  keyWord: undefined,
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RentContractApi.getRentContractPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await RentContractApi.deleteRentContract(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>